package com.ruoyi.web.controller.gd;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gd.ywd.domain.vo.ZYwdVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gd.khxx.domain.vo.ZKhxxVo;
import com.ruoyi.gd.khxx.domain.bo.ZKhxxBo;
import com.ruoyi.gd.khxx.service.IZKhxxService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息
 *
 * @author ruoyi
 * @date 2024-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gd/khxx")
public class ZKhxxController extends BaseController {

    private final IZKhxxService iZKhxxService;

    /**
     * 查询客户信息列表
     */
    @SaCheckPermission("gd:khxx:list")
    @GetMapping("/list")
    public TableDataInfo<ZKhxxVo> list(ZKhxxBo bo, PageQuery pageQuery) {
        return iZKhxxService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户信息列表
     */
    @SaCheckPermission("gd:khxx:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZKhxxBo bo, HttpServletResponse response) {
        List<ZKhxxVo> list = iZKhxxService.queryList(bo);
        for (int i = 0; i < list.size(); i++) {
            ZKhxxVo zKhxxVo = list.get(i);
            zKhxxVo.setExcelId(i + 1 + "");
        }
        ExcelUtil.exportExcel(list, "客户信息", ZKhxxVo.class, response);
    }

    /**
     * 导出选择数据
     */
    @Log(title = "业务单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelectDat/{ids}")
    public void exportSelectData(@PathVariable("ids") String ids, HttpServletResponse response) {
        List<ZKhxxVo> list = iZKhxxService.queryListByIds(ids);
        for (int i = 0; i < list.size(); i++) {
            ZKhxxVo zKhxxVo = list.get(i);
            zKhxxVo.setExcelId(i + 1 + "");
        }
        ExcelUtil.exportExcel(list, "业务单", ZKhxxVo.class, response);
    }


    /**
     * 获取客户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gd:khxx:query")
    @GetMapping("/{id}")
    public R<ZKhxxVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iZKhxxService.queryById(id));
    }

    /**
     * 获取客户的最新的编号
     *
     */
    @GetMapping("/getMaxBh")
    public int getMaxBh() {
        return iZKhxxService.queryByMaxBh();
    }

    /**
     * 新增客户信息
     */
    @SaCheckPermission("gd:khxx:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZKhxxBo bo) {
        return toAjax(iZKhxxService.insertByBo(bo));
    }

    /**
     * 修改客户信息
     */
    @SaCheckPermission("gd:khxx:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZKhxxBo bo) {
        return toAjax(iZKhxxService.updateByBo(bo));
    }

    /**
     * 删除客户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gd:khxx:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZKhxxService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 恢复客户信息
     *
     * @param ids 主键串
     */
    @GetMapping("/recover/{ids}")
    public R<Void> recoverData(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZKhxxService.recoverData(Arrays.asList(ids)));
    }

    /**
     * 重新授权数据
     *
     * @param ids 主键串
     */
    @Log(title = "重新授权数据", businessType = BusinessType.DELETE)
    @GetMapping("/authData/{ids}")
    public R<Void> authData(@PathVariable String[] ids) {
        return toAjax(iZKhxxService.authData(ids));
    }

}
